package com.zxzhu.joyread.View;

import android.app.ProgressDialog;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.graphics.Bitmap;
import android.net.Uri;
import android.support.annotation.NonNull;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;

import com.zxzhu.joyread.Common.BaseActivity;
import com.zxzhu.joyread.Presenter.IRegisterPresenter;
import com.zxzhu.joyread.Presenter.RegisterPresenter;
import com.zxzhu.joyread.R;

public class RegisterView extends BaseActivity implements IRegisterView {

    private final static String TAG = "RegisterViewTest";
    private static final int GET_IMAGE = 1;
    private static final int GET_FIXED_IMAGE = 2;
    public static final int GET_DISK_PERMISSION = 1;
    private IRegisterPresenter presenter;
    private EditText userEdit,pswEdit;
    private Button registerButton;
    private ImageView imageView;
    private ProgressDialog dialog;
    private String bitmapPath = null;


    @Override
    protected void initData() {
        presenter = new RegisterPresenter(this);
        userEdit = $(R.id.register_username);
        pswEdit = $(R.id.register_password);
        imageView = $(R.id.register_image);
        registerButton = $(R.id.register_registerButton);
        setImageView();
        setRegisterButton();
    }

    private void setImageView() {
        imageView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                //包括权限申请
                presenter.sendGetImageIntent(GET_IMAGE);
            }
        });
    }

    private void setRegisterButton() {
        registerButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                String username = userEdit.getText().toString();
                String password = pswEdit.getText().toString();
                if(!username.isEmpty() && !password.isEmpty()){
                    presenter.register(username,password,bitmapPath);
                }else{
                    toast("用户名或密码不能为空！");
                }
            }
        });
    }

    @Override
    protected int getContentViewId() {
        return R.layout.activity_register_view;
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        switch (requestCode) {
            case GET_IMAGE:
                if (data != null) {
                    Uri uri = data.getData();
                    Log.d(TAG,uri.toString());
                    presenter.sendGetFixedIntent(uri,GET_FIXED_IMAGE);
                }
                break;
            case GET_FIXED_IMAGE:
                Bitmap bitmap = data.getParcelableExtra("data");
                if(bitmap == null) break;
                presenter.setImageView(bitmap);
                bitmapPath = presenter.getFixedImagePath(bitmap);
                Log.d(TAG,bitmapPath);
                break;
        }
    }

    @Override
    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
        super.onRequestPermissionsResult(requestCode, permissions, grantResults);
        switch (requestCode) {
            case GET_DISK_PERMISSION:
                if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED){ //同意权限申请
                    toast("申请权限成功！");
                }else { //拒绝权限申请
                    toast("没有获取到权限哦..");
                }
                break;
            default:
                break;
        }
    }

    @Override
    public ImageView getImageView() {
        return imageView;
    }

    @Override
    public void showDialog() {
        if(dialog == null) dialog = new ProgressDialog(this);
        dialog.setProgressStyle(ProgressDialog.STYLE_SPINNER);// 设置进度条的形式为圆形转动的进度条
        dialog.setCancelable(true);// 设置是否可以通过点击Back键取消
        dialog.setCanceledOnTouchOutside(true);// 设置在点击Dialog外是否取消Dialog进度条
        dialog.setTitle("正在注册");
        dialog.setMessage("稍等");
        dialog.show();
    }

    @Override
    public void hideDialog() {
        if(dialog == null) return;
        dialog.hide();
    }

    @Override
    public void setClick() {

    }
}
